const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
//const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const SpeedMeasureWebpackPlugin = require('speed-measure-webpack-plugin');
const glob = require("glob");//文件路径的匹配模块
const PurgeCssWebpackPlugin = require('purgecss-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PATHS = {
    src: path.join(__dirname, 'src')
}
//const smw = new SpeedMeasureWebpackPlugin();
//console.log(glob.sync(`${PATHS.src}/**/*`,{nodir:true}));
module.exports = {
    mode: 'production',
    devtool: false,
    entry: {
        page1: './src/page1.js',
        page2: './src/page2.js',
        page3: './src/page3.js'
    },
    cache: {
         type: 'memory',  //'memory' | 'filesystem'
         cacheDirectory: path.resolve(__dirname, 'node_modules/.cache/webpack'),
     },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
        //publicPath:'https://img.zhufengpeixun.com/'
        //library:'ajax',//导出的库的名字
        //libraryExport:'chromeAjax',//配置导出的模块哪些子模块需要被导出，只针对libraryTarget是commonjs 和commonjs2
        //var commonjs commonjs2 this window global amd umd
        //libraryTarget:'umd'//配置以何种方式导出库，枚举类型 var commonjs amd umi
    },
    resolve: {
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
        alias: {//如果配置了别名，那么在查找模块的时候，会直接使用别名里提供的绝对路径 
            'bootstrap': path.resolve('node_modules/bootstrap/dist/css/bootstrap.css')
        },
        modules: ['mymodules', 'node_modules'],
        //web webworker ['browser','module','main']
        // ['module','main']
        mainFields: ['chrome', 'browser', 'module', 'main'],
        mainFiles: ['entry', 'index']
    },
    resolveLoader: {
        modules: ['loaders', 'node_modules']
    },
    module: {
        //如果使用noParse的话，jquery模块里不能使用import require define
        noParse: /jquery|lodash/,//parser为了查找依赖项编译依赖项
        rules: [
            {
                test: /\.js$/,
                use: [
                    /* {
                        loader: 'thread-loader',
                        options: { workers: 3 }
                    }, */
                    /* {
                        loader:'cache-loader'
                    }, */
                     {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                //modules:false的意思是在loader转换的时候，不要把import 语句export转换成commonjs
                                ["@babel/preset-env",{modules:false}]
                            ],
                            cacheDirectory:true//开启缓存
                        }
                    }
                ]

            },
            {
                test: /\.css$/,
                include: path.join(__dirname, 'src'),
                exclude: /node_modules/,
                use: [
                    'style-loader',//不能配MiniCssExtractPlugin.loader
                    'css-loader'
                ]
            }
        ]
    },
    /* externals:{
        './title':'TITLE'
    }, */
    //stats:'verbose',
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
       /*  new MiniCssExtractPlugin({
            filename: '[name].css'
        }),
        new PurgeCssWebpackPlugin({
            //** 代表所有的目录和文件名，包括路径分隔符/,*包含所有的文件名，不包含/
            paths: glob.sync(`${PATHS.src}/`, { nodir: true })
        }) */
        /* new webpack.IgnorePlugin({
            resourceRegExp: /(^\.\/locale)|(^\.\/locale)/
        }), */
        //new BundleAnalyzerPlugin()
        //new FriendlyErrorsWebpackPlugin()
        //1.1参数是匹配引入模块路径的正则表达式
        //2.2参数是匹配模块的对应的上下文目录，也就是目录名
    ]
}